package cn.iocoder.yudao.module.cont.controller.admin.base;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.cont.controller.admin.base.vo.*;
import cn.iocoder.yudao.module.cont.dal.dataobject.base.PlanInfoDO;
import cn.iocoder.yudao.module.cont.service.base.PlanInfoService;

@Tag(name = "管理后台 - 合同收付款计划表 ")
@RestController
@RequestMapping("/cont/plan-info")
@Validated
public class PlanInfoController {

    @Resource
    private PlanInfoService planInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建合同收付款计划表 ")
    @PreAuthorize("@ss.hasPermission('cont:plan-info:create')")
    public CommonResult<Long> createPlanInfo(@Valid @RequestBody PlanInfoSaveReqVO createReqVO) {
        return success(planInfoService.createPlanInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新合同收付款计划表 ")
    @PreAuthorize("@ss.hasPermission('cont:plan-info:update')")
    public CommonResult<Boolean> updatePlanInfo(@Valid @RequestBody PlanInfoSaveReqVO updateReqVO) {
        planInfoService.updatePlanInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除合同收付款计划表 ")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cont:plan-info:delete')")
    public CommonResult<Boolean> deletePlanInfo(@RequestParam("id") Long id) {
        planInfoService.deletePlanInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得合同收付款计划表 ")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cont:plan-info:query')")
    public CommonResult<PlanInfoRespVO> getPlanInfo(@RequestParam("id") Long id) {
        PlanInfoDO planInfo = planInfoService.getPlanInfo(id);
        return success(BeanUtils.toBean(planInfo, PlanInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得合同收付款计划表 分页")
    @PreAuthorize("@ss.hasPermission('cont:plan-info:query')")
    public CommonResult<PageResult<PlanInfoRespVO>> getPlanInfoPage(@Valid PlanInfoPageReqVO pageReqVO) {
        PageResult<PlanInfoDO> pageResult = planInfoService.getPlanInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PlanInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出合同收付款计划表  Excel")
    @PreAuthorize("@ss.hasPermission('cont:plan-info:export')")
    @OperateLog(type = EXPORT)
    public void exportPlanInfoExcel(@Valid PlanInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PlanInfoDO> list = planInfoService.getPlanInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "合同收付款计划表 .xls", "数据", PlanInfoRespVO.class,
                        BeanUtils.toBean(list, PlanInfoRespVO.class));
    }

}